#include "formdcsplayer.h"
#include "ui_formdcsplayer.h"

#include <QDebug>

FormDCSPlayer::FormDCSPlayer(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::FormDCSPlayer)
{
    struct masterParam param;

    ui->setupUi(this);
    setWindowFlags(windowFlags() | Qt::Dialog);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_DeleteOnClose);

    //展示播放列表
    mDbopt = new DCSDbOperator();
    QList<QString> lists;
    mDbopt->getPlayLists(lists);
    for (int i = 0; i < lists.size(); i++)
    {
        QListWidgetItem *item = new QListWidgetItem();
        item->setText(lists.at(i));
        ui->listWidget->addItem(item);
    }

    if (mDbopt->getMasterParameter(param))
    {
        if (param.fps > 100)
        {
            qDebug() << "FPS to large, set to 25";
            param.fps = 25;
        }
        ui->comboBoxFPS->setCurrentText(QString("").setNum(param.fps));

        if (param.playMode == DCSPlayer::SingleCycle)
        {
            ui->comboBoxPlayMode->setCurrentIndex(0);
        }
        else if (param.playMode == DCSPlayer::ListsCycle)
        {
            ui->comboBoxPlayMode->setCurrentIndex(1);
        }
        else
        {
            ui->comboBoxPlayMode->setCurrentIndex(0);
        }
    }


    qDebug() << "[FormDCSPlayer] \t Constructor";
}

FormDCSPlayer::~FormDCSPlayer()
{
    delete mDbopt;
    delete ui;
    qDebug() << "[FormDCSPlayer] \t Destructor";
}

void FormDCSPlayer::on_pushButtonReturn_clicked()
{
    this->close();
}

void FormDCSPlayer::on_pushButtonPlaylistSelect_clicked()
{
    mFSelWin = new FormDCSFileSelector(this);
    mFSelWin->show();
    connect(mFSelWin, &FormDCSFileSelector::winClose, this, &FormDCSPlayer::onChildWinClose);
}

void FormDCSPlayer::onChildWinClose()
{
    QList<QString> lists;
    mDbopt->getPlayLists(lists);
    ui->listWidget->clear();

    //节目选择子窗口退出，刷新播放列表
    for (int i = 0; i < lists.size(); i++)
    {
        QListWidgetItem *item = new QListWidgetItem();
        item->setText(lists.at(i));
        ui->listWidget->addItem(item);
    }
}

void FormDCSPlayer::on_pushButtonPlayPause_clicked()
{
    mPlayWin = new FormPlay(this);
    mPlayWin->show();
}

void FormDCSPlayer::on_comboBoxFPS_currentIndexChanged(int index)
{
    qDebug() << ui->comboBoxFPS->currentText();
    quint8 fsp = ui->comboBoxFPS->currentText().toUInt();
    if (fsp > 100)
    {
        fsp = 25;
        qDebug() << "FPS to large, set to 25";
    }

    mDbopt->setMasterParameterFPS(fsp);
}

void FormDCSPlayer::on_comboBoxPlayMode_currentIndexChanged(int index)
{
    if (index == 0)
    {
        mDbopt->setMasterParameterPlayMode(0);
    }
    else
    {
        mDbopt->setMasterParameterPlayMode(1);
    }
}
